package org.bjtu.hadooptest.hadoop;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;


public class FsUrl {
    static {
        URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
    }

    public static void cat(String remoteFilePath) {
        try (InputStream in = new URL("hdfs", "172.31.42.153", 9000, remoteFilePath)
                .openStream()) {
            IOUtils.copyBytes(in, System.out, 4096, false);
            IOUtils.closeStream(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 主函数
     */
    public static void main(String[] args) {
        String remoteFilePath = "/user/root/text.txt"; // HDFS路径

        try {
            System.out.println("读取文件: " + remoteFilePath);
            FsUrl.cat(remoteFilePath);
            System.out.println("\n读取完成");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    作者：Tiny_16
//    链接：https://www.jianshu.com/p/0663d74b79b5
//    來源：简书
//    著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。
}
